package cn.taobaoke.restful.controller.admin;

import cn.taobaoke.restful.controller.BaseController;
import cn.taobaoke.restful.interceptor.Auth;
import cn.taobaoke.restful.service.StatisticsService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author yangzuo
 * @since 2017-03-27
 */
@RestController
@RequestMapping("/{domain}/{domainId}/api/admins")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class StatisticsEndpoint extends BaseController {

    @Auth(type = Auth.AuthType.ADMIN)
    @GetMapping("/statics/today")
    public Result staticsToady(@PathVariable String domain, @PathVariable Long domainId) {
        Map map = statisticsService.staticsAdminToday(domain, domainId);
        return wrapObj(map);
    }

    @Auth(type = Auth.AuthType.ADMIN)
    @GetMapping("/subAgents/income")
    public Result getAdminSubAgentIncome(@PathVariable String domain, @PathVariable Long domainId, String keyword, String start, String end, Integer page, Integer size) {
        keyword = keyword == null ? "" : keyword;
        end = end == null ? DateTime.now().toString("yyyy-MM-dd") : end;
        start = start == null ? DateTime.now().plusDays(-7).toString("yyyy-MM-dd") : start;

        page = page == null || page < 1 ? 1 : page; // 注意: 外部传入的是从1开始, 需要换成从0开始计数
        size = size == null || size < 0 ? 20 : size;

        List<Map> results = statisticsService.queryAdminSubAgentIncome(keyword, start, end, page - 1, size, domain, domainId);
        Long total = statisticsService.countAgent(keyword, domainId);
        return wrapPage(page - 1, size, total, results);
    }

    @Auth(type = Auth.AuthType.ADMIN)
    @GetMapping("/orderAndIncome")
    public Result getAdminOrderAndIncome(@PathVariable String domain, String start, String end) {
        List<Map> results = statisticsService.queryAdminOrderCountAndIncome(start, end, domain);
        return wrapObj(results);
    }

    @Auth(type = Auth.AuthType.ADMIN)
    @GetMapping("/agents")
    public Result getAdminAgents(@PathVariable String domain, @PathVariable Long domainId, String start, String end) {
        List<Map> results = statisticsService.queryAdminAgentCount(start, end, domainId);
        return wrapObj(results);
    }

    @Auth(type = Auth.AuthType.ADMIN)
    @GetMapping("/members")
    public Result getAdminMembers(@PathVariable String domain, @PathVariable Long domainId, String start, String end) {
        List<Map> results = statisticsService.queryAdminMemberCount(start, end, domainId);
        return wrapObj(results);
    }

    private final @NonNull
    StatisticsService statisticsService;
}
